package com.andromouse;

import java.awt.AWTException;
import java.awt.Robot;
import java.io.IOException;

public class SystemOperations {
	public boolean isWin;
	public static final int LOGOFF = 1;
	public static final int LOCK = 2;
	public static final int RESTART = 3;
	public static final int SLEEP = 4;
	public static final int SHUTDOWN = 5;

	public void doSystemOperation(int commandcode) throws IOException,
			AWTException {
		String myCommand = null;
		String operatingSystem = System.getProperty("os.name");
		if (operatingSystem.contains("Win")) {
			this.isWin = true;
			System.out.println("Win");
		}
		switch (commandcode) {
		case 1:
			if (this.isWin)
				myCommand = "shutdown -l -f";
			else
				myCommand = "sudo logout";
			break;
		case 3:
			if (this.isWin)
				myCommand = "shutdown -r";
			else
				myCommand = "sudo shutdown -r";
			break;
		case 4:
			if (this.isWin)
				myCommand = "rundll32 powrprof.dll,SetSuspendState";
			else
				myCommand = "sudo shutdown -s";
			break;
		case 2:
			if (!this.isWin)
				break;
			lockWin();
		case 5:
			if (this.isWin)
				myCommand = "shutdown -s";
			else {
				myCommand = "sudo shutdown -h now";
			}
		}
		if (myCommand != null)
			Runtime.getRuntime().exec(myCommand);
	}

	public void lockWin() throws AWTException {
		Robot robot = new Robot();
		robot.keyPress(157);
		robot.keyPress(76);
		robot.keyRelease(157);
		robot.keyRelease(76);

		System.out.println("lock");
	}
}